Completed
Push — develop ( 7c8456...1887a3 )
by Xaver
32s
created

proportions.js ➔ ... ➔ hostnameOfNodeID   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 7
rs 9.4285
nop 1
1
define(['d3-interpolate', 'snabbdom', 'filters/genericnode', 'helper'],
2
  function (d3Interpolate, V, Filter, helper) {
3
    'use strict';
4
5
    return function (config, filterManager) {
6
      var self = this;
7
      var scale = d3Interpolate.interpolate('#770038', '#dc0067');
8
      V = V.default;
9
10
      var statusTable;
11
      var fwTable;
12
      var hwTable;
13
      var geoTable;
14
      var autoTable;
15
      var gatewayTable;
16
      var gateway6Table;
17
      var siteTable;
18
19
      function count(nodes, key, f) {
20
        var dict = {};
21
22
        nodes.forEach(function (d) {
23
          var v = helper.dictGet(d, key.slice(0));
24
25
          if (f !== undefined) {
26
            v = f(v);
27
          }
28
29
          if (v === null) {
30
            return;
31
          }
32
33
          dict[v] = 1 + (v in dict ? dict[v] : 0);
34
        });
35
36
        return Object.keys(dict).map(function (d) {
37
          return [d, dict[d], key, f];
38
        });
39
      }
40
41
      function addFilter(filter) {
42
        return function () {
43
          filterManager.addFilter(filter);
44
          return false;
45
        };
46
      }
47
48
      function fillTable(name, table, data) {
49
        if (!table) {
50
          table = document.createElement('table');
51
        }
52
53
        var max = Math.max.apply(Math, data.map(function (o) {
54
          return o[1];
55
        }));
56
57
        var items = data.map(function (d) {
58
          var v = d[1] / max;
59
60
          var filter = new Filter(_.t(name), d[2], d[0], d[3]);
61
62
          var a = V.h('a', { props: { href: '#' }, on: { click: addFilter(filter) } }, d[0]);
63
64
          var th = V.h('th', a);
65
          var td = V.h('td', V.h('span', {
66
            style: {
67
              width: 'calc(25px + ' + Math.round(v * 90) + '%)',
68
              backgroundColor: scale(v)
69
            }
70
          }, d[1].toFixed(0)));
71
72
          return V.h('tr', [th, td]);
73
        });
74
        var tableNew = V.h('table', { props: { className: 'proportion' } }, items);
75
        return V.patch(table, tableNew);
76
      }
77
78
      self.setData = function setData(data) {
79
        var onlineNodes = data.nodes.online;
80
        var nodes = onlineNodes.concat(data.nodes.lost);
81
82
        function hostnameOfNodeID(nodeid) {
83
          var gateway = data.nodeDict[nodeid];
84
          if (gateway) {
85
            return gateway.hostname;
86
          }
87
          return null;
88
        }
89
90
        var gatewayDict = count(nodes, ['gateway'], hostnameOfNodeID);
91
        var gateway6Dict = count(nodes, ['gateway6'], hostnameOfNodeID);
92
93
        var statusDict = count(nodes, ['is_online'], function (d) {
94
          return d ? 'online' : 'offline';
95
        });
96
        var fwDict = count(nodes, ['firmware', 'release']);
97
        var hwDict = count(nodes, ['model']);
98
        var geoDict = count(nodes, ['location'], function (d) {
99
          return d && d.longitude && d.latitude ? _.t('yes') : _.t('no');
100
        });
101
102
        var autoDict = count(nodes, ['autoupdater'], function (d) {
103
          if (d === null) {
104
            return null;
105
          } else if (d.enabled) {
106
            return d.branch;
107
          }
108
          return _.t('node.deactivated');
109
        });
110
111
        var siteDict = count(nodes, ['nodeinfo', 'site_code'], function (d) {
112
          if (config.siteNames) {
113
            config.siteNames.forEach(function (t) {
114
              if (d === t.site) {
115
                d = t.name;
116
              }
117
            });
118
          }
119
          return d;
120
        });
121
122
        statusTable = fillTable('node.status', statusTable, statusDict.sort(function (a, b) {
123
          return b[1] - a[1];
124
        }));
125
        fwTable = fillTable('node.firmware', fwTable, fwDict.sort(function (a, b) {
126
          if (b[0] < a[0]) {
127
            return -1;
128
          }
129
          if (b[0] > a[0]) {
130
            return 1;
131
          }
132
          return 0;
133
        }));
134
        hwTable = fillTable('node.hardware', hwTable, hwDict.sort(function (a, b) {
135
          return b[1] - a[1];
136
        }));
137
        geoTable = fillTable('node.visible', geoTable, geoDict.sort(function (a, b) {
138
          return b[1] - a[1];
139
        }));
140
        autoTable = fillTable('node.update', autoTable, autoDict.sort(function (a, b) {
141
          return b[1] - a[1];
142
        }));
143
        gatewayTable = fillTable('node.selectedGatewayIPv4', gatewayTable, gatewayDict.sort(function (a, b) {
144
          return b[1] - a[1];
145
        }));
146
        gateway6Table = fillTable('node.selectedGatewayIPv6', gateway6Table, gateway6Dict.sort(function (a, b) {
147
          return b[1] - a[1];
148
        }));
149
        siteTable = fillTable('node.site', siteTable, siteDict.sort(function (a, b) {
150
          return b[1] - a[1];
151
        }));
152
      };
153
154
      self.render = function render(el) {
155
        self.renderSingle(el, 'node.status', statusTable);
156
        self.renderSingle(el, 'node.firmware', fwTable);
157
        self.renderSingle(el, 'node.hardware', hwTable);
158
        self.renderSingle(el, 'node.visible', geoTable);
159
        self.renderSingle(el, 'node.update', autoTable);
160
        self.renderSingle(el, 'node.selectedGatewayIPv4', gatewayTable);
161
        self.renderSingle(el, 'node.selectedGatewayIPv6', gateway6Table);
162
        self.renderSingle(el, 'node.site', siteTable);
163
164
        if (config.globalInfos) {
165
          var images = document.createElement('div');
166
          el.appendChild(images);
167
          var img = [];
168
          config.globalInfos.forEach(function (globalInfo) {
169
            img.push(V.h('h2', globalInfo.name));
170
            img.push(helper.showStat(V, globalInfo));
171
          });
172
          V.patch(images, V.h('div', img));
173
        }
174
      };
175
176
      self.renderSingle = function renderSingle(el, heading, table) {
177
        if (table.children.length > 0) {
178
          var h2 = document.createElement('h2');
179
          h2.classList.add('proportion-header');
180
          h2.textContent = _.t(heading);
181
          h2.onclick = function onclick() {
182
            table.elm.classList.toggle('hide');
183
          };
184
          el.appendChild(h2);
185
          el.appendChild(table.elm);
186
        }
187
      };
188
      return self;
189
    };
190
  });
191